package com.glela.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;
import com.glela.serial.model.request.RequestSerial;
import com.glela.serial.model.response.hometitle.ResponseHomeTitleInfo;
import com.glela.serial.service.SerialGoodService;


/**
 * @ClassName: SerialProductController
 * @Description: 期对应商品列表接口逻辑控制器
 * @author zhangrenjie
 * @date 2017年7月6日
 *
 */
@Controller
@RequestMapping(value = "/serialGood", produces = "application/json;charset=UTF-8")
public class SerialGoodController extends BaseController {
	@Autowired
	private SerialGoodService serialGoodService;
	@Autowired
	private UserService userService;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;

	/**
	 * @Title: queryAllProductBySerialID
	 * @Description: 首页专题页期商品查询入口
	 * @param request 参数
	 * @return ResponseEntity<?> 返回类型
	 * @author zhangrenjie
	 * @date 2017年7月6日
	 */
	@RequestMapping(value = "/queryAllProduct", method = RequestMethod.POST)
	public ResponseEntity<?> queryAllProductBySerialID(@RequestBody RequestSerial request) {
		String rslt = null;
		String tempKey = null;
		try {
			if (ParameterUtils.validParams(request.getSerialId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
//			tempKey = getTempCacheKey(request.getSerialId(), request.getUserId(), request.getPageNum(), request.getPageSize());
			//修改缓存key设计
			String _login_user_store_flag = "XZJJ"; //登录店铺的标识，用来区分一般店铺和校妆姐姐店铺.
			Integer storeUserId = this.baseRestrictionsService.getBaseRestrictionsForCache("xz_jj").getExperience();
			User _login_user = null;//登录用户
			if (null != request.getUserId() && request.getUserId() > 0) {
				_login_user = this.userService.getUserById(request.getUserId());
				if (_login_user != null && StringUtils.isNotEmpty(_login_user.getRecommendFrom())) {
					String recommendFrom = _login_user.getRecommendFrom();//登录人对应的店主身份ID
					User userInfo = this.userService.getUserByPersionId(recommendFrom);//推荐人身份
					if (userInfo != null) {
						if (userInfo.getId() != (long) storeUserId) {
							_login_user_store_flag = "STORE";
						}
					}
				}
			}
			tempKey = getTempCacheKey(request.getSerialId(), _login_user_store_flag, request.getStartIndex(), request.getPageSize());
			rslt = queryCacheResult(tempKey);
			if (StringUtils.isEmpty(rslt)) {
				rslt = toResultModelJSON(this.serialGoodService.queryAllProductIdsBySerialId(request));
			} else {
				return toRespEntity(rslt);
			}
		} catch (Exception e) {
			getLOGGER().error("期对应商品列表接口异常：{}", e.getMessage(), e);
			rslt = toErrorModelJSON(ErrorEnum.C20000);
		}
		return toRespEntity(toCacheResult(tempKey, rslt));
	}
	
	/**
	* **********************************************
	* @Title: serialGoodsPreview 
	* @Description: 专题页预览
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2017年12月12日 下午4:28:40 
	* **********************************************
	 */
	@RequestMapping(value = "/serialGoodsPreview", method = RequestMethod.POST)
	public ResponseEntity<?> serialGoodsPreview(@RequestBody RequestSerial request) {
		try {
			if (ParameterUtils.validParams(request.getSerialId(),request.getPageNum(),request.getPageSize())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			ResponseHomeTitleInfo responseHomeTitleInfo = this.serialGoodService.serialGoodsPreview(request.getSerialId(),request.getStartIndex(),request.getPageSize());
			return new ResponseEntity<>(new ResponseResultModel<>(responseHomeTitleInfo), HttpStatus.OK);
		} catch (Exception e) {
			getLOGGER().error("专题页预览异常：{}", e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

}
